#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 16;
const int H[] = { 1,0,-1,0};
const int L[] = { 0,1,0,-1};

char s[N][N];
int n,m;

namespace F { // max_flow_with_cost
    const int N = 510;
    const int M = N * 4 + 10;
    const int inf = 0x3f3f3f3f;
    int h[N],now[N],cnt = 1,S,T;
    bool vis[N];
    ll maxflow,cost,dis[N];
    struct edge { int v,w,c,nxt; } e[M << 1];
    inline void tlink(int x,int y,int w,int c) { e[++cnt] = (edge) { y,w,c,h[x] }; h[x] = cnt; }
    inline void link(int x,int y,int w,int c) { tlink(x,y,w,c); tlink(y,x,0,-c); }
    inline void setst(int s,int t) { S = s; T = t; }
    bool bfs() {
	queue < int > q; q.push(S); memset(dis,0x3f,sizeof dis); memset(vis,0,sizeof vis); dis[S] = 0; now[S] = h[S]; vis[S] = 1;
	while(!q.empty()) {
	    int x = q.front(); q.pop();
	    for(int i = h[x],y;i;i = e[i].nxt)
		if(e[i].w && dis[y = e[i].v] > dis[x] + e[i].c) {
		    dis[y] = dis[x] + e[i].c; now[y] = h[y];
		    if(!vis[y]) vis[y] = 1,q.push(y);
		}
	    vis[x] = 0;
	} return dis[T] < inf;
    }
    int dinic(int x,int flow,ll c) {
	if(x == T) return maxflow += flow,cost += c * flow,flow;
	int res = flow; vis[x] = 1;
	for(int i = now[x],y;i && res;i = e[i].nxt) {
	    now[x] = i;
	    if(e[i].w && dis[y = e[i].v] == dis[x] + e[i].c && !vis[y]) {
		int k = dinic(y,min(res,e[i].w),c + e[i].c); 
		e[i].w -= k; e[i ^ 1].w += k; res -= k;
	    }
	} vis[x] = 0;
	return flow - res;
    }
    void runit() { while(bfs()) dinic(S,inf,0); }
};

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    n = in,m = in; for(int i = 1;i <= n;i++) scanf("%s",s[i] + 1);
    unordered_map < char , int > p; p['R'] = 1; p['L'] = 3; p['U'] = 2; p['D'] = 0;
    auto id = [] (int a,int b) { return (a - 1) * m + b; };
    F :: setst(0,id(n,m) * 2 + 1); int al = id(n,m);
    for(int i = 1;i <= n;i++) {
	for(int j = 1;j <= m;j++) {
	    int d = p[s[i][j]];
	    for(int k = 0;k < 4;k++) {
		int x = i + H[k],y = j + L[k]; x = (x + n - 1) % n + 1;y = (y + m - 1) % m + 1;
		F :: link(id(i,j),al + id(x,y),1,k != d);
	    }
	}
    }
    for(int i = 1;i <= n;i++)
	for(int j = 1;j <= m;j++) {
	    F :: link(F :: S,id(i,j),1,0);
	    F :: link(id(i,j) + al,F :: T,1,0);
	}
    F :: runit(); printf("%lld\n",F :: cost); return 0;
}
